####################################################################
#***2019 analysis
## Packages
# To install and open the R packages that you need for this code. 
need <- c('tidyverse','readstata13','lfe','glue','rdrobust', 'stargazer','arm', 'broom', 'ggplot2', 'dotwhisker', 'gridExtra', 'ggeffects')
have <- need %in% rownames(installed.packages()) 
if(any(!have)) install.packages(need[!have]) 
invisible(lapply(need, library, character.only=T)) 

# Change path to whereever you place the models
# To set up the working directory. 
script_folder = dirname(rstudioapi::getSourceEditorContext()$path)
setwd(glue('{script_folder}'))
rm(list = ls())
setwd("../")

## load in RD data:
load("5prepdata/final_rd_data.RData")

cf.rd <- rd.data

#https://www.ncsl.org/research/about-state-legislatures/chart-of-term-limits-states.aspx

# chamber == 9 == house

tls <- read_csv("1data/term_limits.csv") %>% filter(year != "n/a") %>% 
  mutate(term_limit = 1) %>% 
  mutate(year = as.numeric(year))

rd.data <- rd.data %>% mutate(state = str_to_upper(state))

rd.data <- rd.data %>% left_join(tls, by = c("state", "year", "chamber")) %>% 
  ungroup %>% 
  group_by(state, chamber) %>% 
  arrange(year) %>% 
  fill(term_limit, .direction=c("down"))
  #dplyr::select(state, year, chamber, term_limit) %>% 
  #View

rd.data$term_limit[is.na(rd.data$term_limit)] <- 0


## descriptives on term limits?
rd.data %>% 
  group_by(state) %>% 
  distinct(year, .keep_all=T) %>% 
  ungroup %>% 
  group_by(prof.rank.cat) %>% 
  summarize(term_limits = sum(term_limit),
            total=n())


## how many candidates does effect produce?
sum(rd.data$won_election)
# 22452
sum(rd.data$won_election[rd.data$won_election == 1 & rd.data$victory_marg <= rd.data$bandwidth])
# 12380

sum(rd.data$won_election[rd.data$won_election == 1 & rd.data$victory_marg <= rd.data$bandwidth & rd.data$ever_runhouse == 1])
# 518

sum(rd.data$won_election[rd.data$won_election == 1 & rd.data$victory_marg <= rd.data$bandwidth & rd.data$ever_winhouse == 1])
# 213

length(seq(1968, 2008, by = 2))
# 21 possible election cycles
518/21
# 25 candidates per cycle
213/21
# 10 candidates winning an election per cycle
10/435
# 2% of house


main.specification.addl <- function(x, limited) {
  rd.data <- filter(rd.data, term_limit == limited)
  
  rd.data$cutoff<-NULL
  rd.data$bandwidth<-NULL
  rd.data$kw<-NULL
  
  y<-rdbwselect(x, rd.data$victory_marg, p=1, c = 0,  kernel = "tri", bwselect="mserd", covs = rd.data$running_terms_served + rd.data$dem + rd.data$rep + rd.data$spc_elec + rd.data$term_length + rd.data$number_candidates + rd.data$year)
  rd.data$bandwidth<-y$bws[1]
  rm(y)
  
  # gen cutoff = 0
  rd.data$cutoff <- 0
  # gen kw = 1-(abs(cutoff-victory_marg))/bandwidth
  rd.data$kw <- 1-(abs(rd.data$cutoff-rd.data$victory_marg))/rd.data$bandwidth
  # replace kw = 0 if (victory_marg>cutoff+bandwidth | victory_marg<cutoff-bandwidth) 
  rd.data$kw[rd.data$victory_marg>(rd.data$cutoff+rd.data$bandwidth) | rd.data$victory_marg<(rd.data$cutoff-rd.data$bandwidth)] <- 0
  out <- felm(x[rd.data$kw>0] ~ won_election  + victory_marg + f_x_win + running_terms_served + dem + rep + spc_elec + term_length + number_candidates | year + state | 0 | state, data = rd.data[rd.data$kw>0,], weights = rd.data$kw[rd.data$kw>0])
  rd.data$mainefx<-NULL
  rd.data$interaction<-NULL
  bw <- round(rd.data$bandwidth[1], 2)
  
  return(list(out, bw))
}


m1<-main.specification.addl(rd.data$ever_ranprim_h[rd.data$term_limit==1], 1)
m2<-main.specification.addl(rd.data$ever_ranprim_h[rd.data$term_limit==0], 0)
m3<-main.specification.addl(rd.data$ever_winprim_h[rd.data$term_limit==1], 1)
m4<-main.specification.addl(rd.data$ever_winprim_h[rd.data$term_limit==0], 0)
m5<-main.specification.addl(rd.data$ever_runhouse[rd.data$term_limit==1], 1)
m6<-main.specification.addl(rd.data$ever_runhouse[rd.data$term_limit==0], 0)
m7<-main.specification.addl(rd.data$ever_winhouse[rd.data$term_limit==1], 1)
m8<-main.specification.addl(rd.data$ever_winhouse[rd.data$term_limit==0], 0)

stargazer(list(m1[[1]], m2[[1]], m3[[1]], m4[[1]]),
          title = "Effect of state legislative service and term limits: Primary elections",
          style="apsr",
          font.size =  "scriptsize",
          model.numbers=T,
          out="7tex/manuscript/tables/sourcefiles/Appendix Table 8.tex",
          label = "tab:term_limits_prim",
          keep.stat=c("n","rsq"),
          dep.var.labels  = "Ever:",
          #column.labels=c("Ran for House primary//(term limits)", "Ran for House primary//(no term limits)", "Won House primary (term limits)", "Won House primary (no term limits)", "Ran for House general (term limits)", "Ran for House general (no term limits)", "Won House general (term limits)", "Won House general (no term limits)"),
          column.labels = rep(c("Ran for House Primary", "Won House Primary"), each = 2),
          add.lines = list(c("Sample:", rep(c("Term Limited", "Not Term Limited"), 2)),
                           c("Bandwidth", m1[[2]], m2[[2]], m3[[2]], m4[[2]])),
          keep = c("won_election"), #order = c(1, 3, 2),
          covariate.labels = c("Won"), 
          notes = "\\parbox[t]{\\linewidth}{Note: This table reports the same model specifications as in Table \\ref{tab:main_results}, but splits the sample by states with or without term limits. The dependent variable is equal to one if the candidate ever runs in the election listed in the column header and is zero otherwise. The sample contains all first-time state legislative elections within the optimal bandwidth based on the \\cite{CCT} algorithm. All regressions include state and election year fixed effects, a linear term in the election margin as well as its interaction with the indicator for having won, and the full set of candidate and election controls. Estimations are triangular kernel-weighted.  Standard errors clustered by state are reported in parentheses.}"
)


stargazer(list(m5[[1]], m6[[1]], m7[[1]], m8[[1]]),
          title = "Effect of state legislative service and term limits: General elections",
          style="apsr",
          font.size =  "scriptsize",
          model.numbers=T,
          out="7tex/manuscript/tables/sourcefiles/Appendix Table 9.tex",
          label = "tab:term_limits_gen",
          keep.stat=c("n","rsq"),
          dep.var.labels  = "Ever:",
          #column.labels=c("Ran for House primary//(term limits)", "Ran for House primary//(no term limits)", "Won House primary (term limits)", "Won House primary (no term limits)", "Ran for House general (term limits)", "Ran for House general (no term limits)", "Won House general (term limits)", "Won House general (no term limits)"),
          column.labels = rep(c("Ran for House General", "Won House General"), each = 2),
          add.lines = list(c("Sample:", rep(c("Term Limited", "Not Term Limited"), 2)),
                           c("Bandwidth", m5[[2]], m6[[2]], m7[[2]], m8[[2]])),
          keep = c("won_election"), #order = c(1, 3, 2),
          covariate.labels = c("Won"), 
          notes = "\\parbox[t]{\\linewidth}{Note: This table reports the same model specifications as in Table \\ref{tab:main_results}, but splits the sample by states with or without term limits. The dependent variable is equal to one if the candidate ever runs in the election listed in the column header and is zero otherwise. The sample contains all first-time state legislative elections within the optimal bandwidth based on the \\cite{CCT} algorithm. All regressions include state and election year fixed effects, a linear term in the election margin as well as its interaction with the indicator for having won, and the full set of candidate and election controls. Estimations are triangular kernel-weighted.  Standard errors clustered by state are reported in parentheses.}"
)


## basic term limit interaction 
rd.data$wonXtl <- rd.data$won_election*rd.data$term_limit

main.specification.tl <- function(x) {
  #rd.data <- filter(rd.data, term_limit == limited)
  
  rd.data$cutoff<-NULL
  rd.data$bandwidth<-NULL
  rd.data$kw<-NULL
  
  y<-rdbwselect(x, rd.data$victory_marg, p=1, c = 0,  kernel = "tri", bwselect="mserd", covs = rd.data$running_terms_served + rd.data$dem + rd.data$rep + rd.data$spc_elec + rd.data$term_length + rd.data$number_candidates + rd.data$year)
  rd.data$bandwidth<-y$bws[1]
  rm(y)
  
  # gen cutoff = 0
  rd.data$cutoff <- 0
  # gen kw = 1-(abs(cutoff-victory_marg))/bandwidth
  rd.data$kw <- 1-(abs(rd.data$cutoff-rd.data$victory_marg))/rd.data$bandwidth
  # replace kw = 0 if (victory_marg>cutoff+bandwidth | victory_marg<cutoff-bandwidth) 
  rd.data$kw[rd.data$victory_marg>(rd.data$cutoff+rd.data$bandwidth) | rd.data$victory_marg<(rd.data$cutoff-rd.data$bandwidth)] <- 0
  out <- felm(x[rd.data$kw>0] ~ won_election + wonXtl + term_limit + victory_marg + f_x_win + running_terms_served + dem + rep + spc_elec + term_length + number_candidates | year + state | 0 | state, data = rd.data[rd.data$kw>0,], weights = rd.data$kw[rd.data$kw>0])
  rd.data$mainefx<-NULL
  rd.data$interaction<-NULL
  
  bw <- round(rd.data$bandwidth[1], 2)
  
  return(list(out, bw))
}



## term limits sub sample interaction


rd.data$mds1_std<-(rd.data$mds1-mean(rd.data$mds1, na.rm=TRUE))/sd(rd.data$mds1, na.rm=TRUE)
rd.data$wonXMDSstd <- rd.data$won_election*rd.data$mds1_std

ever.tl <- rd.data %>% 
  ungroup %>% 
  group_by(state, chamber) %>% 
  filter(any(term_limit)==0)


main.specification.tlint <- function(x) {
  #ever.tl <- filter(ever.tl, term_limit == limited)
  
  ever.tl$cutoff<-NULL
  ever.tl$bandwidth<-NULL
  ever.tl$kw<-NULL
  
  y<-rdbwselect(x, ever.tl$victory_marg, p=1, c = 0,  kernel = "tri", bwselect="mserd", covs = ever.tl$running_terms_served + ever.tl$dem + ever.tl$rep + ever.tl$spc_elec + ever.tl$term_length + ever.tl$number_candidates + ever.tl$year)
  ever.tl$bandwidth<-y$bws[1]
  rm(y)
  
  # gen cutoff = 0
  ever.tl$cutoff <- 0
  # gen kw = 1-(abs(cutoff-victory_marg))/bandwidth
  ever.tl$kw <- 1-(abs(ever.tl$cutoff-ever.tl$victory_marg))/ever.tl$bandwidth
  # replace kw = 0 if (victory_marg>cutoff+bandwidth | victory_marg<cutoff-bandwidth) 
  ever.tl$kw[ever.tl$victory_marg>(ever.tl$cutoff+ever.tl$bandwidth) | ever.tl$victory_marg<(ever.tl$cutoff-ever.tl$bandwidth)] <- 0
  out <- felm(x[ever.tl$kw>0] ~ won_election + wonXMDSstd + mds1_std + victory_marg + f_x_win + running_terms_served + dem + rep + spc_elec + term_length + number_candidates | year + state | 0 | state, data = ever.tl[ever.tl$kw>0,], weights = ever.tl$kw[ever.tl$kw>0])
  ever.tl$mainefx<-NULL
  ever.tl$interaction<-NULL
  
  bw <- round(ever.tl$bandwidth[1], 2)
  
  return(list(out, bw))
}


m1 <- main.specification.tlint(ever.tl$ever_ranprim_h)
m2 <- main.specification.tlint(ever.tl$ever_winprim_h)
m3 <- main.specification.tlint(ever.tl$ever_runhouse)
m4 <- main.specification.tlint(ever.tl$ever_winhouse)


stargazer(list(m1[[1]], m2[[1]], m3[[1]], m4[[1]]),
          title = "Effect of state legislative service among states with term limits",
          style="apsr",
          font.size =  "scriptsize",
          model.numbers=T,
          out="7tex/manuscript/tables/sourcefiles/Appendix Table 16.tex",
          label = "tab:interactions",
          keep.stat=c("n","rsq"),
          dep.var.labels  = "Ever:",
          column.labels=c("Ran for House primary", "Won House primary", "Ran for House general", "Won House general"),
          keep = c("won_election", "mds1_std", "wonXMDSstd"), order = c(1, 3, 2),
          covariate.labels = c("Won", "Professionalism", "Ever Won x Professionalism"), 
          add.lines = list(c("Bandwidth", m1[[2]], m2[[2]], m3[[2]], m4[[2]])),
          notes = "\\parbox[t]{\\linewidth}{Note: This table reports the same model specifications as in Table \\ref{tab:main_results}, but subsets the sample to only those states that ever adopt term limits. The dependent variable is equal to one if the candidate ever runs in the election listed in the column header and is zero otherwise. The sample contains all first-time state legislative elections within the subsample and the optimal bandwidth based on the \\cite{CCT} algorithm. All regressions include state and election year fixed effects, a linear term in the election margin as well as its interaction with the indicator for having won, and the full set of candidate and election controls. Estimations are triangular kernel-weighted.  Standard errors clustered by state are reported in parentheses.}"
)

#### ideology ####

#(obj - mean(obj))/sd(obj)
cf.rd <- rd.data %>% filter(!is.na(abs.recipient.cfscore))

cf.rd$cf.abs.std <- 0
cf.rd$cf.abs.std <- (cf.rd$abs.recipient.cfscore - mean(cf.rd$abs.recipient.cfscore, na.rm=T))/sd(cf.rd$abs.recipient.cfscore, na.rm=T)

cf.rd$wonXcf <- cf.rd$won_election*cf.rd$abs.recipient.cfscore
cf.rd$wonXcfscore <- cf.rd$won_election*cf.rd$recipient.cfscore

# mean zero centered score
cf.rd$wonXcf.std <- cf.rd$won_election * cf.rd$cf.abs.std




### split sample by party:
main.specification.cfabs.party <- function(x, p) {
  cf.rd <- filter(cf.rd, party == p & !is.na(party))
  
  cf.rd$cutoff<-NULL
  cf.rd$bandwidth<-NULL
  cf.rd$kw<-NULL
  
  y<-rdbwselect(x, cf.rd$victory_marg, p=1, c = 0,  kernel = "tri", bwselect="mserd", covs = cf.rd$running_terms_served + cf.rd$dem + cf.rd$rep + cf.rd$spc_elec + cf.rd$term_length + cf.rd$number_candidates + cf.rd$year + cf.rd$cf.abs.std + cf.rd$wonXcf.std)
  cf.rd$bandwidth<-y$bws[1]
  rm(y)
  
  # gen cutoff = 0
  cf.rd$cutoff <- 0
  # gen kw = 1-(abs(cutoff-victory_marg))/bandwidth
  cf.rd$kw <- 1-(abs(cf.rd$cutoff-cf.rd$victory_marg))/cf.rd$bandwidth
  # replace kw = 0 if (victory_marg>cutoff+bandwidth | victory_marg<cutoff-bandwidth) 
  cf.rd$kw[cf.rd$victory_marg>(cf.rd$cutoff+cf.rd$bandwidth) | cf.rd$victory_marg<(cf.rd$cutoff-cf.rd$bandwidth)] <- 0
  
  out <- felm(x[cf.rd$kw>0] ~ won_election + wonXcf.std + cf.abs.std + victory_marg + f_x_win + running_terms_served + dem + rep + spc_elec + term_length + number_candidates | year + state | 0 | state, data = cf.rd[cf.rd$kw>0,], weights = cf.rd$kw[cf.rd$kw>0])
  #out <- lm(x[cf.rd$kw>0] ~ won_election + won_election + wonXMDSstd + mds1_std + victory_marg + f_x_win + running_terms_served + dem + rep + spc_elec + term_length + number_candidates + as.factor(year) + const, data = cf.rd[cf.rd$kw>0,], weights = cf.rd$kw[cf.rd$kw>0])
  bw <- round(cf.rd$bandwidth[1], 2)
  
  return(list(out, bw))
}

cf.rd$party <- NA
cf.rd$party[word(cf.rd$cand_party, 1)=="Republican"] <- "R"
cf.rd$party[word(cf.rd$cand_party, 1)=="Democratic"] <- "D"

m1<-main.specification.cfabs.party(cf.rd$ever_ranprim_h[cf.rd$party=="R" & !is.na(cf.rd$party)], "R")
m2<-main.specification.cfabs.party(cf.rd$ever_winprim_h[cf.rd$party=="R" & !is.na(cf.rd$party)], "R")
m3<-main.specification.cfabs.party(cf.rd$ever_runhouse[cf.rd$party=="R" & !is.na(cf.rd$party)], "R")
m4<-main.specification.cfabs.party(cf.rd$ever_winhouse[cf.rd$party=="R" & !is.na(cf.rd$party)], "R")

m5<-main.specification.cfabs.party(cf.rd$ever_ranprim_h[cf.rd$party=="D" & !is.na(cf.rd$party)], "D")
m6<-main.specification.cfabs.party(cf.rd$ever_winprim_h[cf.rd$party=="D" & !is.na(cf.rd$party)], "D")
m7<-main.specification.cfabs.party(cf.rd$ever_runhouse[cf.rd$party=="D" & !is.na(cf.rd$party)], "D")
m8<-main.specification.cfabs.party(cf.rd$ever_winhouse[cf.rd$party=="D" & !is.na(cf.rd$party)], "D")


stargazer(list(m1[[1]], m2[[1]], m3[[1]], m4[[1]]),
          title = "Effect of state legislative service and ideological extremity by party, Republicans",
          style="apsr",
          font.size =  "scriptsize",
          model.numbers=T,
          out="7tex/manuscript/tables/sourcefiles/Appendix Table 15.tex",
          label = "tab:extreme_R",
          keep.stat=c("n","rsq"),
          dep.var.labels  = "Ever:",
          #column.labels=c("Ran for House primary//(term limits)", "Ran for House primary//(no term limits)", "Won House primary (term limits)", "Won House primary (no term limits)", "Ran for House general (term limits)", "Ran for House general (no term limits)", "Won House general (term limits)", "Won House general (no term limits)"),
          column.labels = c("Ran for House Primary", "Won House Primary", "Run for House General", "Win House General"),
          #add.lines = list(c("Sample:", rep(c("Term Limited", "Not Term Limited"), 2))),
          keep = c("won_election", "cf.abs.std", "wonXcf.std"), order = c(1, 3, 2),
          covariate.labels = c("Won", "Ideological Extremity", "Won x Extremity"), 
          add.lines = list(c("Bandwidth", m1[[2]], m2[[2]], m3[[2]], m4[[2]])),
          notes = "\\parbox[t]{\\linewidth}{Note: This table reports the same model specifications as in Table \\ref{tab:main_results}, but limits the sample based on availability of CF scores for ideologically from \\citet{bonica}. The median year is 2002 and the data coverage is from 1990-2008. The CF scores, after taking the absolute value, have been standardized to have mean zero. The dependent variable is equal to one if the candidate ever runs in the election listed in the column header and is zero otherwise. The sample contains all first-time state legislative elections within the optimal bandwidth based on the \\cite{CCT} algorithm. All regressions include state and election year fixed effects, a linear term in the election margin as well as its interaction with the indicator for having won, and the full set of candidate and election controls. Estimations are triangular kernel-weighted.  Standard errors clustered by state are reported in parentheses.}"
)


stargazer(list(m5[[1]], m6[[1]], m7[[1]], m8[[1]]),
          title = "Effect of state legislative service and ideological extremity by party, Democrats",
          style="apsr",
          font.size =  "scriptsize",
          model.numbers=T,
          out="7tex/manuscript/tables/sourcefiles/Appendix Table 14.tex",
          label = "tab:extreme_D",
          keep.stat=c("n","rsq"),
          dep.var.labels  = "Ever:",
          #column.labels=c("Ran for House primary//(term limits)", "Ran for House primary//(no term limits)", "Won House primary (term limits)", "Won House primary (no term limits)", "Ran for House general (term limits)", "Ran for House general (no term limits)", "Won House general (term limits)", "Won House general (no term limits)"),
          column.labels = c("Ran for House Primary", "Won House Primary", "Run for House General", "Win House General"),
          #add.lines = list(c("Sample:", rep(c("Term Limited", "Not Term Limited"), 2))),
          keep = c("won_election", "cf.abs.std", "wonXcf.std"), order = c(1, 3, 2),
          covariate.labels = c("Won", "Ideological Extremity", "Won x Extremity"), 
          add.lines = list(c("Bandwidth", m5[[2]], m6[[2]], m7[[2]], m8[[2]])),
          notes = "\\parbox[t]{\\linewidth}{Note: This table reports the same model specifications as in Table \\ref{tab:main_results}, but limits the sample based on availability of CF scores for ideologically from \\citet{bonica}. The median year is 2002 and the data coverage is from 1990-2008. The CF scores, after taking the absolute value, have been standardized to have mean zero. The dependent variable is equal to one if the candidate ever runs in the election listed in the column header and is zero otherwise. The sample contains all first-time state legislative elections within the optimal bandwidth based on the \\cite{CCT} algorithm. All regressions include state and election year fixed effects, a linear term in the election margin as well as its interaction with the indicator for having won, and the full set of candidate and election controls. Estimations are triangular kernel-weighted.  Standard errors clustered by state are reported in parentheses.}"
)




####
main.specification.cf <- function(x) {
  cf.rd$cutoff<-NULL
  cf.rd$bandwidth<-NULL
  cf.rd$kw<-NULL
  
  y<-rdbwselect(x, cf.rd$victory_marg, p=1, c = 0,  kernel = "tri", bwselect="mserd", covs = cf.rd$running_terms_served + cf.rd$dem + cf.rd$rep + cf.rd$spc_elec + cf.rd$term_length + cf.rd$number_candidates + cf.rd$year)
  cf.rd$bandwidth<-y$bws[1]
  rm(y)
  
  # gen cutoff = 0
  cf.rd$cutoff <- 0
  # gen kw = 1-(abs(cutoff-victory_marg))/bandwidth
  cf.rd$kw <- 1-(abs(cf.rd$cutoff-cf.rd$victory_marg))/cf.rd$bandwidth
  # replace kw = 0 if (victory_marg>cutoff+bandwidth | victory_marg<cutoff-bandwidth) 
  cf.rd$kw[cf.rd$victory_marg>(cf.rd$cutoff+cf.rd$bandwidth) | cf.rd$victory_marg<(cf.rd$cutoff-cf.rd$bandwidth)] <- 0
  
  out <- felm(x[cf.rd$kw>0] ~ won_election + wonXcfscore + recipient.cfscore + victory_marg + f_x_win + running_terms_served + dem + rep + spc_elec + term_length + number_candidates | year + state | 0 | state, data = cf.rd[cf.rd$kw>0,], weights = cf.rd$kw[cf.rd$kw>0])
  #out <- lm(x[cf.rd$kw>0] ~ won_election + won_election + wonXMDSstd + mds1_std + victory_marg + f_x_win + running_terms_served + dem + rep + spc_elec + term_length + number_candidates + as.factor(year) + const, data = cf.rd[cf.rd$kw>0,], weights = cf.rd$kw[cf.rd$kw>0])
  bw <- round(cf.rd$bandwidth[1], 2)
  
  return(list(out, bw))
}

m5<-main.specification.cf(cf.rd$ever_ranprim_h)
m6<-main.specification.cf(cf.rd$ever_winprim_h)
m7<-main.specification.cf(cf.rd$ever_runhouse)
m8<-main.specification.cf(cf.rd$ever_winhouse)



stargazer(list(m1[[1]], m2[[1]], m3[[1]], m4[[1]]),
          title = "Effect of state legislative service and ideological extremity",
          style="apsr",
          font.size =  "scriptsize",
          model.numbers=T,
          out="7tex/manuscript/tables/sourcefiles/Appendix Table 21.tex",
          label = "tab:extreme",
          keep.stat=c("n","rsq"),
          dep.var.labels  = "Ever:",
          #column.labels=c("Ran for House primary//(term limits)", "Ran for House primary//(no term limits)", "Won House primary (term limits)", "Won House primary (no term limits)", "Ran for House general (term limits)", "Ran for House general (no term limits)", "Won House general (term limits)", "Won House general (no term limits)"),
          column.labels = c("Ran for House Primary", "Won House Primary", "Run for House General", "Win House General"),
          #add.lines = list(c("Sample:", rep(c("Term Limited", "Not Term Limited"), 2))),
          keep = c("won_election", "cf.abs.std", "wonXcf.std"), order = c(1, 3, 2),
          covariate.labels = c("Won", "Ideological Extremity", "Won x Extremity"),
          add.lines = list(c("Bandwidth", m1[[2]], m2[[2]], m3[[2]], m4[[2]])),
          notes = "\\parbox[t]{\\linewidth}{Note: This table reports the same model specifications as in Table \\ref{tab:main_results}, but limits the sample based on availability of CF scores for ideologically from \\citet{bonica}. The median year is 2002 and the data coverage is from 1990-2008. The CF scores, after taking the absolute value, have been standardized to have mean zero. The dependent variable is equal to one if the candidate ever runs in the election listed in the column header and is zero otherwise. The sample contains all first-time state legislative elections within the optimal bandwidth based on the \\cite{CCT} algorithm. All regressions include state and election year fixed effects, a linear term in the election margin as well as its interaction with the indicator for having won, and the full set of candidate and election controls. Estimations are triangular kernel-weighted.  Standard errors clustered by state are reported in parentheses.}"
)
